<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午10:09
 */

namespace app\index\controller;


use Alipay\Alipayobj;
use app\common\controller\Deviceobj;
use app\common\controller\FeiePrint;
use app\common\model\Agent;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Controller;
use think\Log;

class Alipay extends Controller
{
    private $uid;
    public function testpay(){
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => "2021001104658626", // 填写高级调用功能的app id
            'rsaPrivateKey' => "MIIEogIBAAKCAQEApJWRBX+zbfb7NUMb4ZgjjRG2QTEm1c815xla8debQmouxKUgqzDUWyVHtzmVqyyV+9O0gsxP5O6UPTIQu7GfUkbFv7M42CTLrLJRpBdWhmdmNxJVzP/Sy/pWfaIjdsHQht07qtqyvsDAk7cRVid5PPNM8frOVZ4SV/BmpcAakUUbFU8rWTqb1jKmQeV+psmE/RBV05FVfuFaU16sek/JVK+rD+d2qXeeMdHBwG4ccIEVXI6xJ8A/YBE+W6QLH4RE7UG2kho1PMqlz908NPIWCIQiWkUAiUS0hksm6fD17s0JMTw15DqWn5DBhiF7LQLo4HNPOEPCF55tBcDSSVbqAQIDAQABAoIBADGsgqyqMjmKRZ95tbyM8o3tPpjGgPYfKCGr4WsEq/OLoLnxLL7+adlokxCV7LpRJqNu7hhcGfD6oPsnVS2RGoj2m1CUgx+ZW2+wEjv4X0EJjCwq+IJ64+B0mg4pFZMORCWwOAYFI9W2z2jF5BGgsFLZC+wsyoHEx7yK7jAwntQhDvEERBF2fQPMzgHRdWid8xzMw77oRYze2U/FojxKLWD3EzBUL6sS0SjGATxsyW2mWzBHJ2hXIdqSbTWhSd3gQhH4ipflxA4kYaDOHwq6GBuWA6crJv2cl+1t2yU+/8zq6V4VwkngbKha6t3J0Ds5cjgiQs/kCtVGOmMObgPTRwECgYEA4RNOVs+8u29bXf58Hxba0+6OgYJ7O1GCulNOLzFnsPGu9pqwLybuw8oojwxp+GYvz7KH4xu72cdd5h2C7b0kYI22tyfgMsfKCQVCnS23SK0BCrfUL/zP6VVB/hxMazc2eQJETdJGSGpCrKGxTWHlWKX5pKv0fFOfDuOlzpHsZHkCgYEAuzKSwRnlOmYyN8RF0VMckBiAD+hbQqVq+Du3a4DCsjR0xSKm17KLYwejrzqkcmNCnErKzuG9jD8IP9vJbH+zSxtUKqMDV3a0g6r54QX492yezIBxkDB3nwGKQ9astTiQAryEdK5DUx3BYd1JA9jCyvdgNZKxL2JYuedjbRcEf8kCgYBGhh0a2HbDqE3RsubDew7QL0SZ0f+5A35Z0baC1REvgVsYTDn+XE2eLZ7ty678tVAv3Tn08IC46V5Dt2WKBqT6WA0zcJwwbSeiEfLgey2d6Q+VgypKawXej+dgrw9G3QMFga9TTxrUn+8KVpPOsmkBkOgbMAIoXq9NJAmVI2YVyQKBgHJ+mCvrPrzdTNG3BkzlXI71rT4Ni+PTcP2q6KCj/HEqjFEWxJ0g4wgK4B6I65PbCh610B2BDIH++3izYRsvb+X8LRp24dBZFkeCB5uOH5yVnvrNhKgAPOnP81VStAeTNOG3k/wZqavogyLsSYBEz798VifuNNkaedIaqEFM1r+hAoGAd6T4etumub4gSijbsAsh31dUMEPeEA6wcvF0MRASqBYzsLTmOm0WHbCJa5Od98HuRQC/bSh86GLQXQFiKyi3MiTNm46EDYHZ1ytdWoP0GrCCU/dQLlZr4dzaP2CJeS5NWxdxwlmIBjfl8kIgnbDSA1j7oWR1D/ZyjXXI55HDzqE=",

        );
        $data['app_id']="2021001104658626";
        $data['method']="alipay.trade.create";
        $data['charset']="UTF-8";
        $data['sign_type']="RSA2";
        $data['timestamp']="2020-01-12 01:29:53";
        $data['version']="1.0";
        $data['notify_url']="https://os.wunengkeji.com";
        $data['app_auth_token']="202001BB39e5e089314d4d1ba9326be1bf83aX39";
        $biz['out_trade_no']="H1446-2020011200045314739";
        $biz['total_amount']="0.01";
        $biz['subject']="测试支付";
        $biz['buyer_id']="";
        $biz['alipay_store_id']="";
        $biz['extend_params']['sys_service_provider_id']="2088731222672723";
        $data['biz_content']=json_encode($biz,256);
        $aliObj = new Alipayobj($options);
        $data['app_cert_sn']=$aliObj->getCertSN('-----BEGIN CERTIFICATE-----
MIIEsTCCA5mgAwIBAgIQICABEvi24PgsV9y8LJTC4jANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UE
BhMCQ04xFjAUBgNVBAoMDUFudCBGaW5hbmNpYWwxIDAeBgNVBAsMF0NlcnRpZmljYXRpb24gQXV0
aG9yaXR5MTkwNwYDVQQDDDBBbnQgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5IENs
YXNzIDEgUjEwHhcNMjAwMTExMTcyMzI4WhcNMjIwMTEwMTcyMzI4WjB5MQswCQYDVQQGEwJDTjEt
MCsGA1UECgwk5oms5bee5oCd5oGp572R57uc56eR5oqA5pyJ6ZmQ5YWs5Y+4MQ8wDQYDVQQLDAZB
bGlwYXkxKjAoBgNVBAMMITIwODg3MzEyMjI2NzI3MjMtMjAyMTAwMTEwNDY1ODYyNjCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAKSVkQV/s232+zVDG+GYI40RtkExJtXPNecZWvHXm0Jq
LsSlIKsw1FslR7c5lasslfvTtILMT+TulD0yELuxn1JGxb+zONgky6yyUaQXVoZnZjcSVcz/0sv6
Vn2iI3bB0IbdO6rasr7AwJO3EVYneTzzTPH6zlWeElfwZqXAGpFFGxVPK1k6m9YypkHlfqbJhP0Q
VdORVX7hWlNerHpPyVSvqw/ndql3njHRwcBuHHCBFVyOsSfAP2ARPlukCx+ERO1BtpIaNTzKpc/d
PDTyFgiEIlpFAIlEtIZLJunw9e7NCTE8NeQ6lp+QwYYhey0C6OBzTzhDwheebQXA0klW6gECAwEA
AaOCASkwggElMB8GA1UdIwQYMBaAFHEH4gRhFuTl8mXrMQ/J4PQ8mtWRMB0GA1UdDgQWBBRUdDDF
MU8/1ghozyBkt1SDDT1+DTBABgNVHSAEOTA3MDUGB2CBHAFuAQEwKjAoBggrBgEFBQcCARYcaHR0
cDovL2NhLmFsaXBheS5jb20vY3BzLnBkZjAOBgNVHQ8BAf8EBAMCBsAwLwYDVR0fBCgwJjAkoCKg
IIYeaHR0cDovL2NhLmFsaXBheS5jb20vY3JsMzcuY3JsMGAGCCsGAQUFBwEBBFQwUjAoBggrBgEF
BQcwAoYcaHR0cDovL2NhLmFsaXBheS5jb20vY2E2LmNlcjAmBggrBgEFBQcwAYYaaHR0cDovL2Nh
LmFsaXBheS5jb206ODM0MC8wDQYJKoZIhvcNAQELBQADggEBAJOGsH7ntoMphAxloyANCh5z8AFV
2/nPZUqaFpwhAH5MT/vIp+ZLXfbs85b9x7PWB5N3/oMdjKu7cuaRuscHF8ufO7kDGwqd7cS4s+TB
de9/JaN817M0uSo7JPlDTMoHtMQCNRzI6MqBdzUqAPfhwbNWKvE1iYsOJrhvZnzO/HUtzFMVeety
tmkXE5qG4UjkJgsc4f1joClbacKSEWqSTChnKUeteG+T9gdM2bLBiReQ7w4v7U/iGtyNcoNodAE1
YuwKwnhaOp7IXd56SACW7r63MTWHkt2k8lViER855Zx8klmbZbeAJEXVWPhlyRai31Dn1iEW+J8v
7ayy/8ZBW2A=
-----END CERTIFICATE-----');
        $data['alipay_root_cert_sn']=$aliObj->getRootCertSN('-----BEGIN CERTIFICATE-----
MIIBszCCAVegAwIBAgIIaeL+wBcKxnswDAYIKoEcz1UBg3UFADAuMQswCQYDVQQG
EwJDTjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RDQTAeFw0xMjA3MTQw
MzExNTlaFw00MjA3MDcwMzExNTlaMC4xCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVO
UkNBQzEPMA0GA1UEAwwGUk9PVENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE
MPCca6pmgcchsTf2UnBeL9rtp4nw+itk1Kzrmbnqo05lUwkwlWK+4OIrtFdAqn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T83bDM6824EkUnf+X0iKS95SUNGeeSWK2o/3ALJo5hi7GZr3
U8eLaWAcYizfO99UXMRBPw5PRR+gXGEronGUugLpxsjuynoLQu8GQAeysSXKbN1I
UugDo9u8igJORYA+5ms0s5sCUySqbQ2R5z/GoceyI9LdxIVa1RjVX8pYOj8JFwtn
DJN3ftSFvNMYwRuILKuqUYSHc2GPYiHVflDh5nDymCMOQFcFG3WsEuB+EYQPFgIU
1DHmdZcz7Llx8UOZXX2JupWCYzK1XhJb+r4hK5ncf/w8qGtYlmyJpxk3hr1TfUJX
Yf4Zr0fJsGuv
-----END CERTIFICATE-----');

//        var_dump($data);
//        exit();
//        $sign=$aliObj->rsaSign($data, $signType = "RSA2");
//     var_dump($sign);
//     exit();
        $data['sign'] = $aliObj->rsaSign($data, $signType = "RSA2");
//       var_dump($data);
//       exit();
        $uid = $aliObj->getuid($data);
        var_dump($uid);


    }
    public function pay()
    {
        $request = request();
        $Merchant = new Merchant();
        $users = $Merchant->where(array('id' => $request->param('id')))->find();
        if ($users['ali_merid'] == '') {
            $this->error('当前商户未开启支付宝支付功能');
        }
        $Agent = new Agent();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],
        );
        $aliObj = new Alipayobj($options);
        if ($request->get('auth_code')) {
            $param['app_id'] = $agent['ali_app_id'];
            $param['method'] = "alipay.system.oauth.token";
            $param['charset'] = "UTF-8";
            $param['sign_type'] = "RSA2";
            $param['version'] = '1.0';
            $param['timestamp'] = date('Y-m-d H:i:s');
            $param['grant_type'] = 'authorization_code';
            $param['code'] = $request->get('auth_code');
            $signContent = $aliObj->rsaSign($param, $signType = "RSA2");

            $param['sign'] = $signContent;
            $uid = $aliObj->getuid($param);
//            var_dump($uid);
//            var_dump($param);
            $user = json_decode($uid, true);
            $uid = $user['alipay_system_oauth_token_response']['user_id'];

//            echo $uid;
            $this->assign("headimg", $users['img']);
            $this->assign('shmc', $users['merchant']);
            $this->assign('id', $request->param('id'));
            $this->assign('uid', $uid);
            $this->assign('mach', $users);
            $this->assign("channel", "alipay");

            return $this->fetch('alipay');

        } else {
            $callback = 'https://' . $_SERVER ['SERVER_NAME'] . url("Alipay/pay", array('id' => $request->param('id')));
            $url = $aliObj->getOauthRedirect($callback, '', 'auth_base');
//        echo $url;
//        exit();
            header("Location: $url");
        }

    }

    public function alipaypay()
    {
        $request = request();
        $Paylog = new Paylog();
        $orderid = request()->post('orderid');
        $norderid = $Paylog->where(array('orderid' => $orderid))->count();
        if ($norderid != 0) {
            echo 1;

        } else {
            $orderid = request()->post('orderid');
//            $orderid="s0000-1123393993".rand(1,999);
            $Merchant = new Merchant();
            $Agent = new Agent();
            $users = $Merchant->where(array('id' => $request->param('id')))->find();
            $agent = $Agent->where(array('id' => $users['agent']))->find();
            $raskey = $agent['ali_app_key'];
            $options = array(
                //'token' => $config ["token"], // 填写你设定的key
                //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
                'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
                'rsaPrivateKey' => $agent['ali_app_key'],

            );
            $aliObj = new Alipayobj($options);

            $price = request()->post('price');
            $buyer_id = request()->post('buyer_id');
            $out_trade_no = $orderid;
            $body = "快捷支付";
            $total_fee = $price;
            $data['app_id'] = $agent['ali_app_id'];
            $data['method'] = "alipay.trade.create";
            $data['charset'] = "UTF-8";
            $data['sign_type'] = "RSA2";
            $data['timestamp'] = date('Y-m-d H:i:s');
            $data['version'] = "1.0";
            $data['notify_url'] = 'https://' . $_SERVER ['SERVER_NAME'];
            $data['app_auth_token'] = $users['ali_merid'];
            $data['biz_content'] = json_encode(array(
                'out_trade_no' => $out_trade_no,
                'total_amount' => $total_fee,
                'subject' => $body,
                'buyer_id' => $buyer_id,
                'alipay_store_id' => $users['ali_store_id'],
//                'store_id'=>I('get.id'),
                'extend_params' => array(
                    'sys_service_provider_id' => $agent['sys_service_provider_id'],
                ),

            ));
            $signContent = $aliObj->rsaSign($data, $signType = "RSA2");


            $data['sign'] = $signContent;

            $uid = $aliObj->getuid($data);
            $user = json_decode($uid, true);

            $orderid = $user['alipay_trade_create_response']['trade_no'];
            $sdata['orderid'] = $out_trade_no;
            $sdata['mid'] = $request->param('id');
            $sdata['openid'] = request()->post('buyer_id');
            $sdata['pay_statu'] = 0;
            $sdata['price'] = $price;
            $sdata['yf_price'] = $price;
            $sdata['add_time'] = time();
            $sdata['pay_fs'] = 1;
            $Paylog = new Paylog();
            $add = $Paylog->allowField(true)->save($sdata);
            return json($orderid);
        }


    }

    public function payover()
    {
        $request = request();
        $Merchant = new Merchant();
        $Agent = new Agent();
        $users = $Merchant->where(array('id' => $request->param('id')))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],

        );
        $aliObj = new Alipayobj($options);
        $appid = $agent['ali_app_id'];
        $data['app_id'] = $appid;
        $data['method'] = 'alipay.trade.query';
        $data['charset'] = "UTF-8";
        $data['sign_type'] = "RSA2";
        $data['timestamp'] = date('Y-m-d H:i:s');
        $data['version'] = "1.0";
        $data['app_auth_token'] = $users['ali_merid'];
        $data['biz_content'] = json_encode(array(
            'out_trade_no' => $request->param('orderid')
        ));
        $signContent = $aliObj->rsaSign($data, $signType = "RSA2");
        $data['sign'] = $signContent;
        $uid = $aliObj->getuid($data);
        $user = json_decode(iconv("GB2312", "UTF-8//IGNORE", $uid), true);
        $payinfos = $user['alipay_trade_query_response'];
        $payinfos['req_url'] = $request->url(true);
//        file_put_contents('/data2/html/payos/exelog/alipayover.txt', json_encode($payinfos), 8);
//        trace( "支付宝支付-查询结果：" . json_encode($payinfos,256),'SysRunLog');
//        Log::write('支付宝支付-查询结果'. json_encode($payinfos,256),'error');
        $orderstatu = $user['alipay_trade_query_response']['trade_status'];

        if ($orderstatu == "TRADE_SUCCESS") {
//            $data['pay_statu'] = 1;
//            $data['orderno'] = $payinfos['trade_no'];
//            $data['pay_time'] = $payinfos['send_pay_date'];
//            $data['payinfo'] = json_encode($payinfos,JSON_UNESCAPED_UNICODE);
//            file_put_contents('/data2/html/osapi/2333.txt',json_encode($data),8);
//            $Paylog = new Paylog();
//            $Paylog->save($data, ['orderid' => $payinfos['out_trade_no']]);
            $orderid = $request->param('orderid');
            $this->order_query($orderid);
            $data['code'] = 0;

        } else {

            $data['code'] = 1;
        }
        return json($data);
    }

    public function order_query($out_trade_no)
    {
        $Paylog = new Paylog();
        $map['orderid'] = $out_trade_no;
        $payinfos2 = $Paylog->where($map)->find();


        $Merchant = new Merchant();
        $Agent = new Agent();
        $users = $Merchant->where(array('id' => $payinfos2['mid']))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],

        );

        $aliObj = new Alipayobj($options);
        $appid = $agent['ali_app_id'];

        $data['app_id'] = $appid;
        $data['method'] = 'alipay.trade.query';
        $data['charset'] = "UTF-8";
        $data['sign_type'] = "RSA2";
        $data['timestamp'] = date('Y-m-d H:i:s');
        $data['version'] = "1.0";
        $data['app_auth_token'] = $users['ali_merid'];
        $data['biz_content'] = json_encode(array(
            'out_trade_no' => $payinfos2['orderid']
        ));
        $signContent = $aliObj->rsaSign($data, $signType = "RSA2");
        $data['sign'] = $signContent;
        $uid = $aliObj->getuid($data);
        $user = json_decode(iconv("GB2312", "UTF-8//IGNORE", $uid), true);
        $payinfos = $user['alipay_trade_query_response'];
        if ($payinfos['trade_status'] == "TRADE_SUCCESS") {
            $datas['pay_statu'] = 1;
            $datas['orderno'] = $payinfos['trade_no'];
            $datas['pay_time'] = $payinfos['send_pay_date'];
            $datas['payinfo'] = json_encode($payinfos);
            $Paylog = new Paylog();
            $Paylog->save($datas, ['id' => $payinfos2['id']]);
            file_put_contents('/data2/html/payos/exelog/orderquery.txt', json_encode($payinfos2), 8);
            if ((int)$payinfos2['tzzt'] == 0) {
                $s = action('Wxauth/sendtemplate', array($payinfos2['mid'], $payinfos2['orderid']));
                $FeiePrint = new FeiePrint();
                $FeiePrint->sendprint($payinfos2['orderid']);
                $Deviceobj = new Deviceobj();
                $s=$Deviceobj->ylb_sendvoice($payinfos2['orderid']);
//                action('Index/sendprint', array($payinfos2));
                $da['tzzt'] = 1;
                $da['tzmsg'] = json_encode($s);
                $Paylog->save($da, ['id' => $payinfos2['id']]);
            }

        }

    }


    public function orderinfo()
    {

        $Adview = new Adview();
        $request = request();
        $ad_type = $Adview->getadtype($request->param('orderid'));
        $Adview->getadvert($request->param('orderid'));
        $Merchant = new Merchant();
        $Agent = new Agent();
        $users = $Merchant->where(array('id' => $request->param('id')))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],

        );
        $aliObj = new Alipayobj($options);

        if ($request->param('auth_code') || request()->get('auth_code')) {
            $param['app_id'] = $agent['ali_app_id'];
            $param['method'] = "alipay.system.oauth.token";
            $param['charset'] = "UTF-8";
            $param['sign_type'] = "RSA2";
            $param['version'] = '1.0';
            $param['timestamp'] = date('Y-m-d H:i:s');
            $param['grant_type'] = 'authorization_code';
            $param['code'] = $request->param('auth_code') ? $request->param('auth_code') : $request->get('auth_code');
            $signContent = $aliObj->rsaSign($param, $signType = "RSA2");

            //$sign2 = $aliObj->alonersaSign($signContent, $raskey, $signType = "RSA", $keyfromfile = false);


            $param['sign'] = $signContent;
            $uid = $aliObj->getuid($param);
            $user = json_decode($uid, true);
            $this->uid = $user['alipay_system_oauth_token_response']['user_id'];
//            var_dump($uid);
        } else {
            $callback = 'https://' . $_SERVER ['SERVER_NAME'] . url("Alipay/orderinfo", array('id' => $request->param('id'), 'orderid' => $request->param('orderid')));
            $url = $aliObj->getOauthRedirect($callback, '', 'auth_userinfo');
            header("Location: $url");
        }


        $maq['orderid'] = $request->param('orderid');
        $Paylog = new Paylog();
        $order = $Paylog->where($maq)->find();
//        echo $this->uid;
//        echo "<br />";
//        echo $order['openid'];
//        exit();

//        if ($this->uid == $order['openid']) {
        $mav['id'] = $request->param('id');
        $Merchant = new Merchant();
        $shmc = $Merchant->where($mav)->value('merchant');


        $this->assign('shmc', $shmc);
        $this->assign('order', $order);
        $this->assign('id', $request->param('id'));
        $this->assign('orderid', $request->param('orderid'));
//        } else {
//            $this->error('非订单支付者无法查看订单详情');
//        }
        return $this->fetch("alipay/orderinfo");
    }

    public function getorder2()
    {
        $request = request();
        if ($request->isAjax()) {
            $sh = $request->post('id');
            $order = "H" . $sh . "-" . date('YmdHis') . rand(10000, 99999);
            echo $order;
        }
    }

    public function getmerid()
    {
        $Merchant = new Merchant();
        $Agent = new Agent();

        $users = $Merchant->where(array('id' => 602))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],

        );
        $aliObj = new Alipayobj($options);
        $param['app_id'] = $agent['ali_app_id'];
        $param['method'] = "alipay.offline.market.shop.batchquery";
        $param['charset'] = "UTF-8";
        $param['sign_type'] = "RSA2";
        $param['version'] = '1.0';
        $param['timestamp'] = date('Y-m-d H:i:s');
        $param['app_auth_token'] = $users['ali_merid'];
        $param['biz_content'] = json_encode(array(
            'page_no' => 1
        ));
        $signContent = $aliObj->rsaSign($param, $signType = "RSA2");
        $param['sign'] = $signContent;
        $uid = $aliObj->getuid($param);
        $user = json_decode($uid, true);
        var_dump($user);

    }

}